package com.xiaoyu.izf.service;

import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.YqfCommissionSerialLog;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.YqfCommissionSerialLogDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class YqfCommissionSerialLogService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private YqfCommissionSerialLogDao yqfCommissionSerialLogDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public YqfCommissionSerialLog add(int amount, double rate, double commission, int sourceUid, int commissionUid, int dayId) {
        YqfCommissionSerialLog log = new YqfCommissionSerialLog();
        log.setAmount(amount);
        log.setRate(rate);
        log.setCommission(commission);
        log.setSourceUid(sourceUid);
        log.setCommissionUid(commissionUid);
        log.setStoreStatsDayId(dayId);
        return yqfCommissionSerialLogDao.save(log);
    }

    public DataTableBean parList(ParamListBean param, UserInfo user) {
        DataTableBean data = new DataTableBean();
        String datetimeTime = param.getParam().get("datetime_time");
        String[] t = datetimeTime.split("至");
        String countSql = "select count(*) from yqf_commission_serial_log csl,yqf_store_stats_day ssd" +
                " where csl.store_stats_day_id=ssd.id" +
                " and ssd.rec_date>='" + t[0] + "' and ssd.rec_date<='" + t[1] + "'";
        String listSql = "SELECT m.merchant_name as merchantName,ifnull(ssd.order_amount,0) as orderAmount,ifnull(ssd.refund_amount,0) as refundAmount," +
                "ifnull(ssd.income,0) as actualAmount,ifnull(csl.amount,0) as joinedAmount,ifnull(csl.rate,0) as commissionRate,ifnull(csl.commission,0) as commission,ssd.rec_date as serialDate " +
                "FROM yqf_commission_serial_log csl,yqf_store_stats_day ssd,yqf_store s,yqf_merchant m " +
                "where csl.store_stats_day_id=ssd.id and ssd.store_id=s.id and s.merchant_id=m.id and ssd.rec_date>='" + t[0] + "' and ssd.rec_date<='" + t[1] + "'";
        if (user.getIsAdmin() == 0) {
            countSql += " and csl.commission_uid=" + user.getId();
            listSql += " and csl.commission_uid=" + user.getId();
        }

        listSql += " limit " + (param.getPageNo() - 1) * param.getPageLength() + "," + param.getPageLength();
        log.info(listSql);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(listSql);
        log.info(countSql);
        int total = jdbcTemplate.queryForObject(countSql, Integer.class) == null ? 0 : jdbcTemplate.queryForObject(countSql, Integer.class);

        data.setsEcho(param.getsEcho());
        data.setiTotalDisplayRecords(total);
        data.setiTotalRecords(total);
        data.setAaData(list);
        return data;
    }

    public DataTableBean list(ParamListBean param) {
        DataTableBean data = new DataTableBean();
        String datetimeTime = param.getParam().get("datetime_time");
        String[] t = datetimeTime.split("至");
        String countSql = "select count(*) from yqf_commission_serial_log csl,yqf_store_stats_day ssd,user_info ui " +
                " where csl.store_stats_day_id=ssd.id and csl.commission_uid=ui.id" +
                " and ssd.rec_date>='" + t[0] + "' and ssd.rec_date<='" + t[1] + "'";
        String listSql = "SELECT m.merchant_name as merchantName,ifnull(ssd.order_amount,0) as orderAmount,ifnull(ssd.refund_amount,0) as refundAmount," +
                "ifnull(ssd.income,0) as actualAmount,ifnull(csl.amount,0) as joinedAmount,ifnull(csl.rate,0) as commissionRate,ifnull(csl.commission,0) as commission," +
                "ssd.rec_date as serialDate,csl.commission_uid as commissionUid,ui.user_name as commissionUserName" +
                " FROM yqf_commission_serial_log csl,yqf_store_stats_day ssd,yqf_store s,yqf_merchant m,user_info ui " +
                " where csl.commission_uid=ui.id and csl.store_stats_day_id=ssd.id and ssd.store_id=s.id and s.merchant_id=m.id and ssd.rec_date>='" + t[0] + "' and ssd.rec_date<='" + t[1] + "'";

        if (StrUtil.isNotBlank(param.getParam().get("commissionUid"))) {
            countSql += " and csl.commission_uid=" + param.getParam().get("commissionUid");
            listSql += " and csl.commission_uid=" + param.getParam().get("commissionUid");
        }

        if (StrUtil.isNotBlank(param.getParam().get("commissionUserName"))) {
            countSql += " and ui.user_name like'%" + param.getParam().get("commissionUserName") + "%'";
            listSql += " and ui.user_name like'%" + param.getParam().get("commissionUserName") + "%'";
        }

        listSql += " limit " + (param.getPageNo() - 1) * param.getPageLength() + "," + param.getPageLength();
        log.info(listSql);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(listSql);
        log.info(countSql);
        int total = jdbcTemplate.queryForObject(countSql, Integer.class) == null ? 0 : jdbcTemplate.queryForObject(countSql, Integer.class);

        data.setsEcho(param.getsEcho());
        data.setiTotalDisplayRecords(total);
        data.setiTotalRecords(total);
        data.setAaData(list);
        return data;
    }
}
